﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Lr.Core.Validation.Fluent
{
    /// <summary>
    /// Helps building an <see cref="IValidationRule"/>.
    /// </summary>
    /// <typeparam name="TValidatable">The type of the <see cref="IValidatable"/>.</typeparam>
    public class ValidationRuleBuilderWithRuleAlgorithm<TValidatable>
        where TValidatable : IValidatable
    {
        /// <summary>
        /// Gets the <see cref="IValidatable"/> that started the process.
        /// </summary>
        internal IValidatable Validatable { get { return _validatable; } }
        private readonly IValidatable _validatable;

        /// <summary>
        /// Gets the error message to be displayed when the rule fails.
        /// </summary>
        internal string ErrorMessage { get { return _errorMessage; } }
        private readonly string _errorMessage;

        /// <summary>
        /// Gets the rule's algorithm that determines whether a <typeparamref name="TValidatable"/> fails the rule or not.
        /// </summary>
        internal Func<TValidatable, bool> RuleAlgorithm { get {return _ruleAlgorithm; } }
        private readonly Func<TValidatable, bool> _ruleAlgorithm;

        /// <summary>
        /// Gets the properties that are held responsible for the rule.
        /// </summary>
        internal IEnumerable<string> Properties { get { return _properties; } }
        private readonly List<string> _properties;

        /// <summary>
        /// Creates a new <see cref="ValidationRuleBuilderWithErrorMessage"/> of <typeparamref name="TValidatable"/> object.
        /// </summary>        
        /// <param name="parent">The parent object from which this object was created.</param>
        /// <param name="ruleAlgorithm">The rule's algorithm that determines whether a <typeparamref name="TValidatable"/> fails the rule or not.</param>
        internal ValidationRuleBuilderWithRuleAlgorithm(ValidationRuleBuilderWithErrorMessage<TValidatable> parent, Func<TValidatable, bool> ruleAlgorithm)
        {
            ThrowIf.Null(() => parent);
            ThrowIf.Null(() => ruleAlgorithm);

            _validatable = parent.Validatable;
            _errorMessage = parent.ErrorMessage;
            _ruleAlgorithm = ruleAlgorithm;
            _properties = new List<string>();
        }

        /// <summary>
        /// Continues the building process for a specific <typeparamref name="TValidatable"/>.
        /// </summary>
        /// <param name="propertySelector">A property of the <see cref="TValidatable"/> that is held responsible for the given rule.</param>
        /// <returns>A <see cref="ValidationBuilderWithRuleAlgorithm"/> of type <typeparamref name="TValidatable"/>.</returns>
        public ValidationRuleBuilderWithRuleAlgorithm<TValidatable> AndProperty<TProperty>(Expression<Func<TValidatable, TProperty>> propertySelector)
        {
            var property = PropertyInfoHelper.GetPropertyInfo<TValidatable, TProperty>(propertySelector);
            var propertyName = property.Name;

            _properties.Add(propertyName);

            return this;
        }

        /// <summary>
        /// Ends the building process for a specific <typeparamref name="TValidatable"/> and adds the rule to the initiating <typeparamref name="TValidatable"/>.
        /// </summary>
        /// <returns></returns>
        public void Build()
        {
            var validationRule = new ValidationRule<TValidatable>(_errorMessage, _properties, _ruleAlgorithm);
            _validatable.AddRule(validationRule);
        }

        /// <summary>
        /// Returns a <see cref="String"/> that represents the object.
        /// </summary>
        /// <returns>A <see cref="String"/> that represents the object.</returns>
        public override string ToString()
        {
            return this.ToDebugString();
        }
    }
}
